/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Message;
import Beans.User;
import Mail.Mail;
import dao.MessageDao;
import dao.UserDao;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**

 @author Marie-Claude
 */
@ManagedBean(name = "reply")
@RequestScoped
public class ReplyMessage implements Serializable {

  private Message message;
  private String username;
  private int sendingUser;
  @ManagedProperty(value = "#{reader}")
  private MessageReaderManager mbReader;
  @ManagedProperty(value = "#{messageManager}")
  private MessageManager mbMessage;
  @ManagedProperty(value = "#{userManager}")
  private UserManager mbUser;

  private String sendTo;
  private int send;

  /**
   Creates a new instance of ReplyMessage
   */
  public ReplyMessage() {
  }

  public MessageReaderManager getMbReader() {
    return mbReader;
  }

  public void setMbReader(MessageReaderManager mbReader) {
    this.mbReader = mbReader;
  }

  public String getSendTo() {
    return sendTo;
  }

  public Message getMessage() {
    return message;
  }

  public void setMessage(Message message) {
    this.message = message;
  }

  public void setSendTo(String sendTo) {
    this.sendTo = sendTo;
  }

  public MessageManager getMbMessage() {
    return mbMessage;
  }

  public void setMbMessage(MessageManager mbMessage) {
    this.mbMessage = mbMessage;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public int getSend() {
    return send;
  }

  public void setSend(int send) {
    this.send = send;
  }

  public UserManager getMbUser() {
    return mbUser;
  }

  public void setMbUser(UserManager mbUser) {
    this.mbUser = mbUser;
  }

  public int getSendingUser() {
    return sendingUser;
  }

  public void setSendingUser(int sendingUser) {
    this.sendingUser = sendingUser;
  }

  @PostConstruct
  public void init() {

    message = new Message();
    message.setRecipientUsername(mbReader.getSendToUsername());
  }

  public String insertMessage() {
    message.setSenderID(mbUser.getUser().getUserID());
    User u = UserDao.getByUsername(mbReader.getSendToUsername());
    message.setRecipientID(u.getUserID());

    MessageDao.insertMessage(message);
    if (u.getIsWarnedByEmail()) {
      Mail.envoyerMail("tpjava3@gmail.com", "Message reçu!", "Vous avez reçu un nouveau message de la part de " + mbUser.getUser().getUsername());
    }

    return "myMessages";
  }
}
